Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[ios] duration to time interval fix #8276

Conversation

fabian-guerra
Copy link
Contributor

Fixes #8264

@fabian-guerra fabian-guerra added the iOS Mapbox Maps SDK for iOS label Mar 3, 2017
@fabian-guerra fabian-guerra added this to the ios-v3.5.0 milestone Mar 3, 2017
@fabian-guerra fabian-guerra self-assigned this Mar 3, 2017
@mention-bot
Copy link

@fabian-guerra, thanks for your PR! By analyzing this pull request, we identified @incanus, @boundsj and @1ec5 to be potential reviewers.

Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few optional changes, but this PR looks good.

{
return duration.count();
std::chrono::nanoseconds nano(duration.count());
std::chrono::seconds sec = std::chrono::duration_cast<std::chrono::seconds>(nano);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, since this is Objective-C++ code, you could condense these lines somewhat by passing duration.count() directly into the duration_cast. You can also use auto instead of declaring the local variable to be of type std::chrono::seconds. What you’ve got is totally reasonable, though.

#import <XCTest/XCTest.h>

#include <mbgl/util/chrono.hpp>
#import "../../darwin/src/NSDate+MGLAdditions.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since NSDate+MGLAdditions.h is shared between iOS and macOS, the tests should be shared as well. Can you move this file to platform/darwin/test/ and add it to macos.xcodeproj?

mbgl::Duration duration = MGLDurationFromTimeInterval(timeInterval);
NSTimeInterval durationTimeInterval = MGLTimeIntervalFromDuration(duration);

mbgl::Duration durationInNanoseconds(5000000000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since chrono.hpp imports <chrono>, you can write using namespace std::chrono_literals; at the top, then use auto expectedDuration = 5000000000ns; or even mbgl::Duration expectedDuration = 5s;.

@1ec5
Copy link
Contributor

1ec5 commented Mar 3, 2017

Actually, sharing the tests between iOS and macOS is pretty important, so if you could take care of that, that’d be 👌.

Copy link
Contributor

@incanus incanus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending @1ec5's recommendations.

@fabian-guerra fabian-guerra force-pushed the fabian-duration-time-interval-fix branch from 4d81add to fabad6f Compare March 4, 2017 00:36
@fabian-guerra fabian-guerra merged commit 1f1cc13 into release-ios-v3.5.0-android-v5.0.0 Mar 4, 2017
@fabian-guerra fabian-guerra deleted the fabian-duration-time-interval-fix branch March 4, 2017 01:11
Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change appears to have added three identical MGLNSDateAdditionsTests.mm files, each in a different directory. There should only be one, under platform/darwin/test/.

{
return duration.count();
std::chrono::nanoseconds nano(duration.count());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation makes the unsafe assumption that mbgl::Duration's count is in nanoseconds. mbgl::Duration is a typedef to std::chrono::steady_clock::duration, and std::chrono::steady_clock does not specify a particular tick period.

In addition, this implementation truncates any fractional seconds in the duration. NSTimeInterval is a floating-point type, and so the conversion should be expected to preserve fractional seconds.

I believe the correct conversion is as follows (but don't trust me -- write some more tests 😄):

NSTimeInterval MGLTimeIntervalFromDuration(mbgl::Duration duration)
{
    std::chrono::duration<NSTimeInterval, std::ratio<1>>(duration).count();
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
iOS Mapbox Maps SDK for iOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants